<template>
	<view>
		<view class="tb">

		</view>

		<view class="dl" @click="login()">
			微信登录
		</view>

		<view class="zc">
			短信验证登录/注册
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//接口前缀
				urlHeat: "", //https://fine-food.xiamu.icu/
				urlHeat2: "", //https://fine-food.xiamu.icu
				urlHeat3: "", //https://fine-food.xiamu.icu
				// urlHeat2: "http://localhost:8080",
				// urlHeat3: "http://localhost:8080",

				nickName: 'Hello', //昵称
				headerUrl: '', //头像
				//微信授权，码
				code: '',
				Authorization: '',
			}
		},
		onLoad() {
			const { hostname } = this.$store.state.moduleCommon;
			this.urlHeat = hostname + '/';
			this.urlHeat2 = hostname;
			this.urlHeat3 = hostname;
		},
		methods: {

			login() {
				// var that = this
				uni.showModal({
					mask: true,
					title: '温馨提示',
					content: '授权微信登录后才能正常使用小程序功能',
					success: res => {
						if (res.confirm) {

							uni.login({
								provider: 'weixin',
								success: res => {
									// console.log('res-login', res);
									//获取到code
									this.code = res.code;
									//调用请求获得token令牌
									this.getUserOpenId()
								}
							})
						} else if (res.cancel) {

						}
					}
				});
			},

			getUserOpenId() {
				//发送后台请求token
				uni.request({
					url: this.urlHeat2 + "/api/users/getUserToken", //
					method: "POST",
					data: {
						"code": this.code
					},
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
					},
					success: (res) => {
						console.log(res.data.data)
						//保存token
						uni.setStorageSync('Authorization', res.data.data);
						// uni.setTabBarItem({
						//    index: 2,
						//    visible:false,
						// })
						// uni.reLaunch({
						// 	url: '/pages/ydl/ydl'
						// });
	                    uni.switchTab({
							url: '/pages/individual/individual'
						});
						
					},
				});
			}
		}
	}
</script>

<style lang="scss">
	.tb {
		width: 200rpx;
		height: 200rpx;
		border-radius: 35px;
		// background-color: rgb(66,189,85);
		margin: 0 auto;
		margin-top: 70rpx;
		// background-image: url('https://img0.baidu.com/it/u=318161020,4205284301&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500');
		background-image: url('~@/static/lxy/美食_汉堡.png');
		background-size: 250rpx;
		background-repeat: no-repeat;
		background-position: -25rpx -30rpx;
	}

	.dl {
		width: 90%;
		height: 120rpx;
		background-color: rgb(66, 189, 85);
		margin: 0 auto;
		margin-top: 100rpx;
		border-radius: 35rpx;
		text-align: center;
		font-size: 45rpx;
		line-height: 120rpx;
		color: white;
	}

	.zc {
		width: 90%;
		height: 120rpx;
		background-color: rgb(66, 189, 85);
		margin: 0 auto;
		margin-top: 30rpx;
		border-radius: 35rpx;
		text-align: center;
		font-size: 45rpx;
		line-height: 120rpx;
		color: white;
	}
</style>
